import store from "./store";
import router from "./router";
import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css' // 引入进度条样式

const whiteList = ['/login','/404']
// 导航前置守卫
router.beforeEach(async(to,from ,next) => {
  NProgress.start()
  const token = store.getters.token
  if(token) {
    // 有token
    if(to.path === '/login') {
      next({path:'/'})
      NProgress.done()
    }else {
      const userInfo = store.state.user.userInfo
      if(Object.keys(userInfo).length <= 0){
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  }else {
    // 没token
    if(whiteList.includes(to.path)) {
      next()
    }else {
      next({path: '/login'})
      NProgress.done()
    }
  }
})

router.afterEach((to,from)=> {
  NProgress.done()
})